#include <iostream>
#include <string>
#include <cerrno>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

static const std::string g_PipeFile = "fifo.ipc";
#define SIZE 1024

int main()
{
    int fd = open(g_PipeFile.c_str(), O_RDONLY);
    if(fd < 0)
    {
        printf("Open error, %d:%s\n", errno, strerror(errno));
        exit(1);
    }

    char buffer[SIZE];

    while(true)
    {
        ssize_t n = read(fd, buffer, (SIZE) - 1);
        
        if(n > 0)
        {
            buffer[n] = '\0';
            printf("Read message success: > %s\n", buffer);
           
        }
        else if(n == 0)
        {
            printf("Client close!\n");
            break;
        } 
        else
        {
            printf("Read error, %d:%s\n", errno, strerror(errno));
        }
    }
    
    close(fd);

    return 0;
}